package com.wanmait.server.controller;

import com.google.common.collect.Maps;
import com.wanmait.common.response.BaseResponse;
import com.wanmait.common.response.StatusCode;
import com.wanmait.common.util.PageUtile;
import com.wanmait.common.util.ValidatorUtil;
import com.wanmait.model.entity.Post;
import com.wanmait.server.service.PostService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Map;

/**
 * @ClassName PostController
 * @Author 羽
 * @Date 2020/12/13 16:27
 * @Version 1.0
 * post岗位相关的操作
 **/
@Controller
@RequestMapping("/post")
public class PostController extends AbstractController{

    //注入service
    @Autowired
    private PostService postService;

    /**
    * TODO
    * @author 羽
    * @date 2020/12/13 19:21
    * post界面的显示数据，模糊查询
    **/
    @RequestMapping("list")
    @RequiresPermissions("post:list")
    @ResponseBody
    public BaseResponse list(@RequestParam Map<String,Object> paramMap){
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> map = Maps.newHashMap();
        try{
            PageUtile page = postService.pageFindAll(paramMap);
            map.put("page",page);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail);
        }
        response.setData(map);
        return response;
    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/16 14:18
    * 添加岗位
    **/
    @RequestMapping("save")
    @RequiresPermissions("post:save")
    @ResponseBody
    public BaseResponse save(@RequestBody @Validated Post post, BindingResult bindingResult){
        BaseResponse response = new BaseResponse(StatusCode.Success);
        //检验，code和name是否为空
        String result = ValidatorUtil.checkResult(bindingResult);
        if(StringUtils.isNotBlank(result)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),result);
        }
        try{
            //获取当前登录操作的用户
            post.setCreateById(getLoginStaffInfo().getId());
            post.setUpdateById(getLoginStaffInfo().getId());
            postService.savePost(post);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }

        return response;
    }
    /**
    * TODO
    * @author 羽
    * @date 2020/12/16 16:04
    * 修改，根据所选则行的id去获取该post的详细信息
    **/
    @RequestMapping("info")
    @RequiresPermissions("post:info")
    @ResponseBody
    public BaseResponse info(Integer id){
        if(id==null||id<0){
            return new BaseResponse(StatusCode.InvalidParams);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String, Object> map = Maps.newHashMap();
        try {
            map.put("post",postService.findById(id));
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail);
        }
        System.out.println(map);
        response.setData(map);
        return response;
    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/16 15:59
    * 修改岗位
    **/
    @RequestMapping("update")
    @RequiresPermissions("post:update")
    @ResponseBody
    public BaseResponse update(@RequestBody @Validated Post post,BindingResult bindingResult){
        System.out.println("进入修改界面");
        BaseResponse response = new BaseResponse(StatusCode.Success);
        //检验，code和name是否为空
        String result = ValidatorUtil.checkResult(bindingResult);
        if(StringUtils.isNotBlank(result)){
            return new BaseResponse(StatusCode.InvalidParams.getCode(),result);
        }
        if(post.getId()==null||post.getId()<0){
            return new BaseResponse(StatusCode.InvalidParams);
        }
        try {
            //获取当前登录操作的用户
            post.setUpdateById(getLoginStaffInfo().getId());
            postService.updatePost(post);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/17 14:48
    * 删除
    **/
    @RequestMapping("deletePost")
    @RequiresPermissions("post:delete")
    @ResponseBody
    public BaseResponse deletePost(@RequestBody Integer[] ids){
        BaseResponse response = new BaseResponse(StatusCode.Success);
        log.info("删除的id：{}", Arrays.asList(ids));
        try{
            postService.deletePost(ids);
        }catch (Exception e){
            response = new BaseResponse(StatusCode.Fail);
        }

        return response;
    }

    /**
    * TODO
    * @author 羽
    * @date 2020/12/27 23:12
    *获取岗位详情
    **/
    @RequestMapping("select")
    @ResponseBody
    public BaseResponse select(){
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {

            Map<String,Object> resMap=Maps.newHashMap();
            resMap.put("list",postService.list());

            response.setData(resMap);
        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }


}
